let jobListModule = (function(){
    // 获取元素
  let   $tableBox = $('.tableBox'),
    $tbody = $tableBox.find('tbody');
    // 从后台获取数据并绑定
    async function bindHtml(){
       let result = await  axios.get('/job/list')
       if( result.code != 0)  return
        let str = ``
        result.data.forEach( item => {
            let {id,name,desc,power} = item
            console.log(item)
            // 权限处理
            str += `
            <tr>
            <td class="w8">${id}</td>
            <td class="w10">${name}</td>
            <td class="w20">${desc}</td>
            <td class="w50">${power}</td>
            <td class="w12" jobId="${id}">
                <a href="javascript:;">编辑</a>
                <a href="javascript:;">删除</a>
            </td>
        </tr> `
        })
        $tbody.html(str)
    }
    // 编辑  删除
       // 基于事件委托实现
       function deletegate(){
        $tbody.click( async ev => {
            let target = ev.target,
            $target = $(target),
            TAG = target.tagName,
            TEXT = target.innerHTML.trim();
            if(TAG === 'A'){
                let $jobId = $target.parent().attr('jobId')
                console.log($jobId)
                if(TEXT === '编辑'){
                    console.log('编辑')
                       window.location.href='jobAdd.html?id=' + $jobId
                       return
                }
                if(TEXT === '删除'){
                    console.log('删除')
                   let flag = confirm(`小主，您确定要删除编号为[${jobId}]的信息吗，此操作不可撤销`)
                   if(!flag)  return
                   let result = await axios.get('/job/delete',{
                      params:{jobId} 
           
                   })
                   if(result.code == 0){
                       alert('小主，已为您成功删除一条数据')
                       $target.parent().parent().remove()
                    //    $checkList = $tbody.find('input[type="checkbox"]')
                       return
                   }
                   alert('当前网络繁忙，请刷新后重试~~~')
                   return
               }
            }
           
        })
    }
    return{
        init(){
         bindHtml()
         deletegate()
        }
    }
})()
jobListModule.init()